//
//  DPasswordView.m
//  FiltrationSystem
//
//  Created by 123456 on 2017/8/10.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "DPasswordView.h"
#import "DetailspageViewModel.h"
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#import "DetailspageViewController.h"
@implementation DPasswordView
{

    NSDictionary *DetailsDict;
    NSString *password;
    NSString *iconestr1;
    NSString *iconestr2;
    NSString *iconestr3;
    NSString *iconestr4;
    NSString *iconestr5;
    NSString *iconestr6;
    int i;

}


- (void)awakeFromNib {
    
    i=0;
    [self layout];


    [self.button1 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button2 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button3 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button4 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button5 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button6 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button7 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button8 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button9 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];
    [self.button0 setBackgroundImage:[UIImage imageNamed:@"框框"] forState:UIControlStateHighlighted];


    

}

- (IBAction)Icone:(UIButton *)sender {
    
    i++;
    
    if (i==1) {
        self.Icone1.image = [UIImage imageNamed:@"小框"];
        iconestr1 = sender.titleLabel.text;
    }else if (i==2){
        self.Icone2.image = [UIImage imageNamed:@"小框"];
       iconestr2 = sender.titleLabel.text;

    }else if (i==3){
        self.Icone3.image = [UIImage imageNamed:@"小框"];
        iconestr3 = sender.titleLabel.text;

    }else if (i==4){
        self.Icone4.image = [UIImage imageNamed:@"小框"];
       iconestr4 = sender.titleLabel.text;

    }else if (i==5){
        self.Icone5.image = [UIImage imageNamed:@"小框"];
        iconestr5 = sender.titleLabel.text;

    }else if (i==6){
        self.Icone6.image = [UIImage imageNamed:@"小框"];
      iconestr6 = sender.titleLabel.text;
    password = [NSString stringWithFormat:@"%@%@%@%@%@%@",iconestr1,iconestr2,iconestr3,iconestr4,iconestr5,iconestr6];

        [self Determine];
    }

    
    
}


-(void)Determine{
    
    DetailsDict = @{@"token":[NSUD objectForKey:@"token"],@"imie":self.imie,@"password":password};

    [DetailspageViewModel requestWithUrlForPasswordIn:self andParam:DetailsDict success:^(NSDictionary *model) {
        
        
        if ([model[@"msg"]isEqualToString:@"管理员密码错误！"]) {
            self.Titlelabel.text = @"密码错误";
            CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
            shake.fromValue = [NSNumber numberWithFloat:-10];
            shake.toValue = [NSNumber numberWithFloat:10];
            shake.duration = 0.1;//执行时间
            shake.autoreverses = YES; //是否重复
            shake.repeatCount = 2;//次数
            [self.Icone1.layer addAnimation:shake forKey:@"shakeAnimation"];
            [self.Icone2.layer addAnimation:shake forKey:@"shakeAnimation"];
            [self.Icone3.layer addAnimation:shake forKey:@"shakeAnimation"];
            [self.Icone4.layer addAnimation:shake forKey:@"shakeAnimation"];
            [self.Icone5.layer addAnimation:shake forKey:@"shakeAnimation"];
            [self.Icone6.layer addAnimation:shake forKey:@"shakeAnimation"];
            self.Icone1.image = [UIImage imageNamed:@"小框框.png"];
            self.Icone2.image = [UIImage imageNamed:@"小框框.png"];
            self.Icone3.image = [UIImage imageNamed:@"小框框.png"];
            self.Icone4.image = [UIImage imageNamed:@"小框框.png"];
            self.Icone5.image = [UIImage imageNamed:@"小框框.png"];
            self.Icone6.image = [UIImage imageNamed:@"小框框.png"];
            i=0;
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

        }else if ([model[@"status"]intValue] ==1){

            [[NSNotificationCenter defaultCenter]postNotificationName:@"Timer"object:nil];

            [UIView animateWithDuration:0.5 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
                UIWindow * window = [UIApplication sharedApplication].keyWindow ;
                [MBProgressHUD showSuccess:@"重置成功" toView:window];
                
                
            }];
        }
    } failure:^(NSString *error) {

    }];
    
}
- (IBAction)cancel:(id)sender {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Timer"object:nil];

    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];
    
}

-(void)layout{
    
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        self.IconeW.constant = 60;
        self.IconeH.constant  =60;
        self.IconeL.constant = -60;
        self.IconeR.constant = -60;
        self.IconeT.constant = 170;
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
        self.IconeW.constant = 70;
        self.IconeH.constant  =70;
        self.IconeL.constant = -70;
        self.IconeR.constant = -70;
        self.IconeT.constant = 190;
    }else{
        
    }
    
}


@end
